package com.ha8.netease.views.utils
{
	import com.ha8.netease.models.WeiboModel;
	import com.ha8.netease.models.vo.MailModel;
	import com.ha8.netease.models.vo.MicroblogModel;
	import com.ha8.netease.models.vo.UserModel;
	
	import mx.collections.ArrayCollection;
	import mx.formatters.DateFormatter;
	
	/**
	 * 数据处理
	 * 
	 * @author rogo
	 */	
	public class DataUtil
	{
		
		/**
		 * 合并存放WeiboModel的两个ArrayCollection
		 * 
		 * @param source
		 * @param target
		 * @return 
		 */		
		public static function merge(source:ArrayCollection, target:ArrayCollection):ArrayCollection
		{
			for each (var item:WeiboModel in target)
			{
				if (hasItem(source, item))
				{
					// 数据存在
				}
				else
				{
					source.addItem(item);
				}
			}
			
			return source;
		}
		
		/**
		 * 是否存在WeiboModel数组中是否存在指定的WebioModel
		 * 
		 * @param source
		 * @param item
		 * @return 
		 */		
		public static function hasItem(source:ArrayCollection, item:WeiboModel):Boolean
		{
			for each (var ele:WeiboModel in source)
			{
				if (ele.equal(item))
					return true;
			}
			
			return false;
		}
		
		/**
		 * 时间类型转换成字符串
		 * 
		 * @param Date
		 * @return 
		 */
		public static function getDateString(d:Date):String
		{
			var dateString:String;
			var now:Date = new Date();
			var interval:Number = now.getFullYear() - d.getFullYear();
			if (interval != 0)
			{
				dateString = formatDate(d, "YYYY-MM-DD HH:NN");
			}
			else
			{
				interval = now.getMonth() - d.getMonth();
				if (interval != 0)
				{
					dateString = formatDate(d, "MM-DD HH:NN");
				}
				else
				{
					interval = now.getDate() - d.getDate();
					if (interval != 0)
					{
						dateString = formatDate(d, "MM-DD HH:NN");
					}
					else
					{
						interval = now.getHours() - d.getHours();
						if (interval != 0)
						{
							dateString = "今天 " + formatDate(d, "HH:NN");
						}
						else
						{
							interval = now.getMinutes() - d.getMinutes();
							if (interval < 0)
							{
								dateString = "今天 " +  formatDate(d, "HH:NN");
							}
							else if (interval > 0)
							{
								dateString = interval + "分钟";
							}
							else if (interval == 0)
							{
								dateString = "1分钟";
							}
						}
					}
				}
			}
			return dateString;
		}
		
		private static function formatDate(d:Date, fmt:String):String
		{
			var format:DateFormatter = new DateFormatter();
			format.formatString = fmt;
			return format.format(d);
		}
		
		/**
		 * 获取正文中的图片路径
		 * @param str
		 */		
		public static function getStatusImg(str:String):String
		{
			var imgRegx:RegExp = /http:\/\/126.fm\/\w*/;
			return imgRegx.exec(str);
		}
		
		/**
		 * 字符串转换成Html
		 * 
		 * @param str
		 * @return 
		 */
		public static function getStatusHtmlText(str:String):String
		{
			getStatusImg(str);
			str = replaceSpace(str);
			str = transformStr(str);
			var imgRegx:RegExp = /http:\/\/126.fm\/\w*/gi;
			var atRegx:RegExp = /@([A-Za-z0-9\u4e00-\u9fa5]+)/gi;
			var keywordRegx:RegExp = /(?<![color='])#([^#]+)#/gi;
			var shortUrlRegx:RegExp = /((http[s]?|ftp)?:\/\/(([\w-]+:)?[\w-]+@)?([\w-]+\.)+[\w-]+(:[\d]+)?(\/[\w-   .\/?%&=]*)?(#[\w,]+)?)/gi;
			str = str.replace(imgRegx, '');
			str = str.replace(shortUrlRegx, '<a href="$1" target="_blank"><font color="#054C86">$1</font></a>');
			str = str.replace(atRegx, '<a href="http://t.sina.com.cn/n/$1" target="_blank"><font color="#054C86">@$1</font></a>');
			str = str.replace(keywordRegx, '<a href="http://t.sina.com.cn/k/$1" target="_blank"><font color="#054C86">#$1#</font></a>');
			return str;
		}
		
		private static function replaceSpace(str:String):String
		{
			return str.replace(/[\s*]+/g, ' ');
		}
		
		private static function transformStr(str:String):String
		{
			str = str.replace(/&quot;/g, "\"");
			str = str.replace(/&amp;/g, "&");
			str = str.replace(/>/g, "&gt;");
			str = str.replace(/</g, "&lt;");
			return str;
		}

		/**
		 * Object 转换为 UserModel
		 * 
		 * @param data
		 * @return UserModel
		 */		
		public static function objToUser(data:Object):UserModel
		{
			return new UserModel(data);
		}
		
		/**
		 * Object 转换 UserModel 数组
		 * 
		 * @param data
		 * @return ［UserModel UserModel UserModel UserModel］
		 */		
		public static function ObjToUsers(data:Object):ArrayCollection
		{
			var tempArr:Array = data as Array;
			
			if (!tempArr)
				return null;
			
			var resultAC:ArrayCollection = new ArrayCollection();
			
			for each (var obj:Object in tempArr)
			{
				resultAC.addItem(new UserModel(obj));
			}
			
			return resultAC;
		}
		
		/**
		 * Object 转换为 MicroblogModel
		 * 
		 * @param data
		 * @return MicroblogModel
		 */		
		public static function objToMicroblog(data:Object):MicroblogModel
		{
			return new MicroblogModel(data);
		}
		
		/**
		 * Object 转换为 MicroblogModel数组
		 * 
		 * @param data
		 * @return ［MicroblogModel MicroblogModel MicroblogModel MicroblogModel］
		 */		
		public static function objToMicroblogs(data:Object):ArrayCollection
		{
			var tempArr:Array = data as Array;
			
			if (!tempArr)
				return null;
			
			var resultAC:ArrayCollection = new ArrayCollection();
			
			for each (var obj:Object in tempArr)
			{
				resultAC.addItem(new MicroblogModel(obj));
			}
			
			return resultAC;
		}
		
		/**
		 * Object 转换为MailModel的数组
		 * 
		 * @param data
		 * @return 
		 */		
		public static function objToMails(data:Object):ArrayCollection
		{
			var tempArr:Array = data as Array;
			
			if (!tempArr)
				return null;
			
			var resultAC:ArrayCollection = new ArrayCollection();
			
			for each (var obj:Object in tempArr)
			{
				resultAC.addItem(new MailModel(obj));
			}
			
			return resultAC;
		}
		
	}
}